package com.zhss.security.demospringsecurity.controller;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.Principal;
import java.util.Collection;

/**
 * @ClassName TestController
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2021/9/9 09:52
 * @Version 1.0
 */
@RestController
public class TestController {

	@Autowired
	private Producer producer;

	@GetMapping("/vc.jpg")
	public void getVerifyCode(HttpServletResponse response,
							  HttpSession httpSession) {
		response.setContentType("image/jpeg");
		String text = producer.createText();
		System.out.println(text);
		httpSession.setAttribute("kaptcha", text);
		BufferedImage image = producer.createImage(text);
		try (ServletOutputStream outputStream = response.getOutputStream()){
			ImageIO.write(image, "jpg", outputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@GetMapping("/hello")
	public String helloWorld() {
		return "Hello world";
	}

	@GetMapping("/admin")
	public String admin() {
		return "fullAuthentication";
	}

	@GetMapping("/remember-me")
	public String rememberMe() {
		return "rememberMe";
	}

	@GetMapping("/index")
	public String index() {
		return "login success";
	}

	@GetMapping("/test")
	public String test() {
		return "myself target url test";
	}

	@GetMapping("/user")
	public void userInfo() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		System.out.println(authentication.getName());//获取用户名
		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();//获取权限信息
		authorities.forEach(System.out::println);
	}

	@GetMapping("/test2")
	public void test2() throws InterruptedException {

		new Thread(() -> {
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			System.out.println(authentication.getName());//获取用户名
			Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();//获取权限信息
			authorities.forEach(System.out::println);
		}).start();
//		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//		System.out.println(authentication.getName());//获取用户名
//		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();//获取权限信息
//		authorities.forEach(System.out::println);
		Thread.sleep(1000);

	}

	@GetMapping("/test3")
	public void test3(Authentication authentication) {
		System.out.println(authentication.getName());
		authentication.getAuthorities().forEach(System.out::println);
	}

	@GetMapping("/test4")
	public void test4(Principal principal, HttpServletRequest request) {
		System.out.println(principal.getName());
//		principal.getAuthorities().forEach(System.out::println);
	}


}
